package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 结算单明细控制器
 */
@Controller
@RequestMapping("business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "business/statementItem/";

    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IStatementService statementService;
    @Autowired
    private IUserService userService;

    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model) {
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement", statement);
        // 已支付
        if (Statement.STATUS_PAID.equals(statement.getStatus())) {
            // 获取收款人并封装到结算单
            statement.setPayee(userService.get(statement.getPayeeId()));
            return prefix + "itemDetail";
        } else {    // 消费中
            return prefix + "itemAdd";
        }
    }

    // http://localhost/business/statementItem/query?statementId=3
    // 查询结算单明细
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo) {
        return statementItemService.query(qo);
    }

    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return AjaxResult.success();
    }

    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId) {
        statementItemService.payStatement(statementId);
        return AjaxResult.success();
    }

    @RequestMapping("/exportStatementItem")
    public void exportStatementItem(Long id, HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 获取请求头
        String header = request.getHeader("User-Agent");
        // 文件名
        String filename = "结算单明细.xlsx";
        // 根据不同浏览器将文件名进行编码
        String name = header.contains("MSIE") ? URLEncoder.encode(filename, StandardCharsets.UTF_8)
                : new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
        // 设置响应头
        response.setHeader("Content-Disposition","attachment;filename=" + name);
        Workbook workbook = statementItemService.exportStatementItem(id);
        // 将工作簿写入输出流
        workbook.write(response.getOutputStream());
    }


}
